'''

Copyright 2010 Manuel Reinelt

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Created on 12.04.2010

@author: Manuel Reinelt
contact: mrdevlab@googlemail.com

Pycrypto is licensed under the Python License

See: http://www.python.org/psf/license/

Further information/homepages:

http://www.dlitz.net/software/pycrypto/
http://www.amk.ca/python/code/crypto.html

Find binaries for Windows here (Thanks Michael Foord!):

http://www.voidspace.org.uk/python/modules.shtml#pycrypto

as well as his excellent infos on Python/IronPython and more:

http://www.voidspace.org.uk/

'''

import sys
import crypt_data
import wipe_file
import os


def crypt_file(key,encdec,cipher,inputfile,outputfile,wipe,verbose):
    '''
    encrypts/decrypts a file using pycrypto
    '''
        
    sourcefile = open(inputfile,"rb")
    destinationfile = open(outputfile,"wb")
    
    #create chunks to prevent memory overload:

    if cipher == "blowfish":
        blocksize = 8
    elif cipher == "aes":
        blocksize = 16
        
    chunksize = blocksize * 1000000
    
    #read and write data chunkwise in size of 8 or 16 MB blocks
    #to prevent memory overload:

    if verbose:
        print encdec + "rypting " + str(os.path.getsize(inputfile)) + " bytes"
        print "reading source"
        sys.stdout.flush()  

    progress = 0
        
    for i in range(0,os.path.getsize(inputfile),chunksize):
        if verbose:
            print "reading chunk from " + str(i) + " to " + str(chunksize + i - 1)
            
        sourcebuffer = sourcefile.read(chunksize)
        if verbose:
            if os.path.getsize(inputfile) > chunksize:
                progress = int(100.0000000000 / os.path.getsize(inputfile) * i)
            else:
                progress = 100
            
            print "chunk encoding-progress: %" + str(progress) + "%"
            sys.stdout.flush()
        
        #encdecbytes = crypt_data.crypt_data(cipher, key, sourcebuffer[i:chunksize + i], encdec, verbose)
        encdecbytes = crypt_data.crypt_data(cipher, key, sourcebuffer, encdec, verbose)
        
        
        if verbose:
            print "writing " + str(chunksize) + " bytes to " + outputfile
            sys.stdout.flush()  
        #write the chunk to destinationfile:
        destinationfile.write(encdecbytes)
             
    #close both source and destinationfile:
    sourcefile.close()
    destinationfile.close()  
    
    #check if file is to be wiped:
    if (wipe):
        wipe_file.wipe_file(inputfile,wipe,chunksize,verbose)
        
        
        